let socket = null;

export function connectWebSocket(messageCallback, errorCallback) {
  socket = new WebSocket('wss://sns.desai.art/wss');

  socket.addEventListener('open', () => {
    // console.log('WebSocket connected');
  });

  socket.addEventListener('message', event => {
    // console.log('Received message:', event.data);
    messageCallback(event.data);
  });

  socket.addEventListener('close', () => {
    // console.log('WebSocket disconnected');
  });

  socket.addEventListener('error', error => {
    // console.error('WebSocket error:', error);
    errorCallback(error);
  });
}

export function disconnectWebSocket() {
  if (socket && socket.readyState === WebSocket.OPEN) {
    socket.close();
    socket = null;
  }
}
  
export function sendMessage(message) {
  if (socket && socket.readyState === WebSocket.OPEN) {
    socket.send(message);
  }
}
